package com.gxs.ea.fileupload.beans.data.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;

import com.gxs.ea.fileportal.domain.BaseCompany;
import com.gxs.ea.fileportal.domain.util.ErrorResponse;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

@Service("companyService")
public class CompanyServiceImpl implements CompanyService 
{
	private static final String BASE_RESOURCE = "http://localhost:8081/sample-rest/companies";
	private static Client client = null;
	static
	{
		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
		client = Client.create(clientConfig);
	}
	
	@Override
	public BaseCompany saveCompany(BaseCompany company) 
	{
		
		WebResource webResource = client.resource(BASE_RESOURCE);
		ClientResponse clientResponse= webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class, company);
		
		if(clientResponse.getStatus() == 200)
		{
			BaseCompany savedCompany = clientResponse.getEntity(BaseCompany.class);
			return savedCompany;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}
	}

	@Override
	public List<BaseCompany> listCompanies() 
	{
		
		WebResource webResource = client.resource(BASE_RESOURCE);
		ClientResponse clientResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		
		if(clientResponse.getStatus() == 200)
		{
			GenericType<ArrayList<BaseCompany>> gt = new GenericType<ArrayList<BaseCompany>>() {};
			List<BaseCompany> list = clientResponse.getEntity(gt);
			
			return list;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}	
	}

	@Override
	public void deleteCompany(BaseCompany company) 
	{
		
		WebResource webResource = client.resource(BASE_RESOURCE + "/" + company.getCompanyCode());
		ClientResponse clientResponse = webResource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class);
		
		if(clientResponse.getStatus() == 200 || clientResponse.getStatus() == 204)
		{
			return;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}
		
	}

	@Override
	public void updateCompany(BaseCompany company) 
	{
		
		WebResource webResource = client.resource(BASE_RESOURCE + "/" + company.getCompanyCode());
		ClientResponse clientResponse = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).put(ClientResponse.class, company);
		
		if(clientResponse.getStatus() == 200 || clientResponse.getStatus() == 204)
		{
			return;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}
	}

	@Override
	public BaseCompany findByCompanyCode(String companyCode) 
	{
		
		WebResource webResource = client.resource(BASE_RESOURCE + "/" + companyCode);
		ClientResponse clientResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		
		if(clientResponse.getStatus() == 200)
		{
			BaseCompany foundCompany = clientResponse.getEntity(BaseCompany.class);
			return foundCompany;
		}
		else if(clientResponse.getStatus() == 404 )
		{
			return null;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}
	}
}
